//
//  HomeDetialHeaderVIew.m
//  Arts1
//
//  Created by Mac Jc on 15/10/4.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "HomeDetialHeaderVIew.h"
#import "AllHeader.h"

//添加/取消收藏图片
#define kURL @"http://arts1.cn/app/collect"

@implementation HomeDetialHeaderVIew

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor = kCustomColor(235, 235, 235, 1);
        
        //
        //        _shadowView = [[UIView alloc] initWithFrame:self.bounds];
        //        _shadowView.backgroundColor = [UIColor whiteColor];
        //        [UIView viewShadowColor:_shadowView];
        //        [self addSubview:_shadowView];
        
        // 背景大图
        _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, _imageViewHeight)];
        _backgroundImageView.layer.cornerRadius = 5;
        _backgroundImageView.layer.masksToBounds = YES;
        _backgroundImageView.userInteractionEnabled = YES;
        _backgroundImageView.backgroundColor = kRandomColor;
        [_backgroundImageView setContentMode:(UIViewContentModeScaleAspectFill)];
        [self addSubview:_backgroundImageView];
        
        
        //  单击查看图片
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
        [tapRecognizer addTarget:self action:@selector(bigButtonTapped:)];
        [_backgroundImageView addGestureRecognizer:tapRecognizer];
        
        
        // 头像
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * WIDTH, _imageViewHeight + 22, 30, 30)];
        _iconView.layer.cornerRadius = 15;
        _iconView.backgroundColor = kRandomColor;
        _iconView.layer.masksToBounds = YES;
        [_iconView setContentMode:(UIViewContentModeScaleAspectFill)];
        [self addSubview:_iconView];
        
        // 标签
        _tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(_iconView.right + 10, _iconView.top , 100 * WIDTH, 14 * HEIGHT)];
        //        titleLable.text = _titleString;
        _tagLabel.textColor = kCustomColor(150, 150, 150, 1);
        _tagLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_tagLabel];
        
        // 作者
        _authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(_iconView.right + 10, _tagLabel.bottom + 3, 100 * WIDTH, 12 * HEIGHT)];
        _authorLabel.textAlignment = NSTextAlignmentLeft;
        _authorLabel.textColor = kCustomColor(40, 40, 50, 1);
        _authorLabel.font = [UIFont boldSystemFontOfSize:10];
        [self addSubview:_authorLabel];
        
        // 收藏
        _collectionButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _collectionButton.frame = CGRectMake(SCREEN_WIDTH - 85 * WIDTH, _iconView.top, 75 * WIDTH, 30 * HEIGHT);
        _collectionButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _collectionButton.layer.masksToBounds = YES;
        //        _collectionButton.backgroundColor = kCustomColor(40, 40, 50, 1);
        [_collectionButton setBackgroundImage:[UIImage imageNamed:@"buttonImage"] forState:(UIControlStateNormal)];
        _collectionButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _collectionButton.tintColor = [UIColor whiteColor];
        [_collectionButton addTarget:self action:@selector(tapToCollect:) forControlEvents:(UIControlEventTouchUpInside)];
        _collectionButton.layer.cornerRadius = 4;
        [UIView viewShadowColor:_collectionButton];
        //        [_collectionButton.layer setMasksToBounds:YES];
        [self addSubview:_collectionButton];
        
        
        
        // 简介
        _describeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _iconView.bottom + 20, SCREEN_WIDTH - 20, 13 * HEIGHT)];
        _describeLabel.textAlignment = NSTextAlignmentLeft;
        _describeLabel.textColor = kCustomColor(80, 80, 80, 1);
        _describeLabel.font = [UIFont systemFontOfSize:13];
        [_describeLabel setNumberOfLines:0];
        _describeLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self addSubview:_describeLabel];
        
        //  查看数次图标
        _viewNumImageView = [[UIImageView alloc] init];
        _viewNumImageView.frame = CGRectMake(SCREEN_WIDTH - 120, _describeLabel.bottom + 20, 15, 12);
        _viewNumImageView.image = [UIImage imageNamed:@"ViewNum"];
        [self addSubview:_viewNumImageView];
        //  查看次数Lebel
        _viewNumLabel = [[UILabel alloc] init];
        _viewNumLabel.frame = CGRectMake(_viewNumImageView.right + 3, _describeLabel.bottom + 20, 40, 11);
        _viewNumLabel.textColor = kCustomColor(180, 180, 180, 1);
        _viewNumLabel.font = [UIFont boldSystemFontOfSize:11];
        //        _viewNumLabel.text = @"57";
        [self addSubview:_viewNumLabel];
        
        //  点赞次数图标
        _goodNumImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_viewNumLabel.right + 3, _viewNumLabel.top, 15, 12)];
        _goodNumImageView.alpha = 0.8;
        _goodNumImageView.image = [UIImage imageNamed:@"tabGoodButton"];
        [self addSubview:_goodNumImageView];
        //  点赞次数Label
        _goodLabel = [[UILabel alloc] initWithFrame:CGRectMake(_goodNumImageView.right + 3, _goodNumImageView.top ,30, 11 )];
        _goodLabel.font = [UIFont boldSystemFontOfSize:11];
        _goodLabel.textColor = kCustomColor(180, 180, 180, 1);
        [self addSubview: _goodLabel];
        
        
        //  评论
        //        _commentNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, self.bottom - 35 , 100 * WIDTH, 20 * HEIGHT)];
        //        _commentNumLabel.textColor = kCustomColor(150, 150, 150, 1);
        //        _commentNumLabel.font = [UIFont boldSystemFontOfSize:13];
        //        [self addSubview:_commentNumLabel];
        //
        //        //  最下方一条线
        //        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bottom - 0.5 * HEIGHT, SCREEN_WIDTH, 0.5)];
        //        _lineView.backgroundColor = kCustomColor(150, 150, 150, 1);
        //        _lineView.alpha = 0.5;
        //        [self addSubview:_lineView];
    }
    return self;
}


//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        self.backgroundColor = kCustomColor(235, 235, 235, 1);
//        
//        //
////        _shadowView = [[UIView alloc] initWithFrame:self.bounds];
////        _shadowView.backgroundColor = [UIColor whiteColor];
////        [UIView viewShadowColor:_shadowView];
////        [self addSubview:_shadowView];
//        
//        // 背景大图
//        _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, _imageViewHeight)];
//        _backgroundImageView.layer.cornerRadius = 5;
//        _backgroundImageView.layer.masksToBounds = YES;
//        _backgroundImageView.userInteractionEnabled = YES;
//        _backgroundImageView.backgroundColor = kRandomColor;
//        [_backgroundImageView setContentMode:(UIViewContentModeScaleAspectFill)];
//        [self addSubview:_backgroundImageView];
//        
//        
//        //  单击查看图片
//        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
//        [tapRecognizer addTarget:self action:@selector(bigButtonTapped:)];
//        [_backgroundImageView addGestureRecognizer:tapRecognizer];
//        
//        
//        // 头像
//        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * WIDTH, _imageViewHeight + 22, 30, 30)];
//        _iconView.layer.cornerRadius = 15;
//        _iconView.backgroundColor = kRandomColor;
//        _iconView.layer.masksToBounds = YES;
//        [_iconView setContentMode:(UIViewContentModeScaleAspectFill)];
//        [self addSubview:_iconView];
//        
//        // 标签
//        _tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(_iconView.right + 10, _iconView.top , 100 * WIDTH, 14 * HEIGHT)];
//        //        titleLable.text = _titleString;
//        _tagLabel.textColor = kCustomColor(150, 150, 150, 1);
//        _tagLabel.font = [UIFont systemFontOfSize:12];
//        [self addSubview:_tagLabel];
//        
//        // 作者
//        _authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(_iconView.right + 10, _tagLabel.bottom + 3, 100 * WIDTH, 12 * HEIGHT)];
//        _authorLabel.textAlignment = NSTextAlignmentLeft;
//        _authorLabel.textColor = kCustomColor(40, 40, 50, 1);
//        _authorLabel.font = [UIFont boldSystemFontOfSize:10];
//        [self addSubview:_authorLabel];
//        
//        // 收藏
//        _collectionButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
//        _collectionButton.frame = CGRectMake(SCREEN_WIDTH - 85 * WIDTH, _iconView.top, 75 * WIDTH, 30 * HEIGHT);
//        _collectionButton.titleLabel.font = [UIFont systemFontOfSize:14];
//        _collectionButton.layer.masksToBounds = YES;
////        _collectionButton.backgroundColor = kCustomColor(40, 40, 50, 1);
//        [_collectionButton setBackgroundImage:[UIImage imageNamed:@"buttonImage"] forState:(UIControlStateNormal)];
//        _collectionButton.titleLabel.font = [UIFont systemFontOfSize:16];
//        _collectionButton.tintColor = [UIColor whiteColor];
//        [_collectionButton addTarget:self action:@selector(tapToCollect:) forControlEvents:(UIControlEventTouchUpInside)];
//        _collectionButton.layer.cornerRadius = 4;
//        [UIView viewShadowColor:_collectionButton];
////        [_collectionButton.layer setMasksToBounds:YES];
//        [self addSubview:_collectionButton];
//        
//        
//        
//        // 简介
//        _describeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _iconView.bottom + 20, SCREEN_WIDTH - 20, 13 * HEIGHT)];
//        _describeLabel.textAlignment = NSTextAlignmentLeft;
//        _describeLabel.textColor = kCustomColor(80, 80, 80, 1);
//        _describeLabel.font = [UIFont systemFontOfSize:13];
//        [_describeLabel setNumberOfLines:0];
//        _describeLabel.lineBreakMode = NSLineBreakByWordWrapping;
//        [self addSubview:_describeLabel];
//        
//        //  查看数次图标
//        _viewNumImageView = [[UIImageView alloc] init];
//        _viewNumImageView.frame = CGRectMake(SCREEN_WIDTH - 120, _describeLabel.bottom + 20, 15, 12);
//        _viewNumImageView.image = [UIImage imageNamed:@"ViewNum"];
//        [self addSubview:_viewNumImageView];
//        //  查看次数Lebel
//        _viewNumLabel = [[UILabel alloc] init];
//        _viewNumLabel.frame = CGRectMake(_viewNumImageView.right + 3, _describeLabel.bottom + 20, 40, 11);
//        _viewNumLabel.textColor = kCustomColor(180, 180, 180, 1);
//        _viewNumLabel.font = [UIFont boldSystemFontOfSize:11];
////        _viewNumLabel.text = @"57";
//        [self addSubview:_viewNumLabel];
//        
//        //  点赞次数图标
//        _goodNumImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_viewNumLabel.right + 3, _viewNumLabel.top, 15, 12)];
//        _goodNumImageView.alpha = 0.8;
//        _goodNumImageView.image = [UIImage imageNamed:@"tabGoodButton"];
//        [self addSubview:_goodNumImageView];
//        //  点赞次数Label
//        _goodLabel = [[UILabel alloc] initWithFrame:CGRectMake(_goodNumImageView.right + 3, _goodNumImageView.top ,30, 11 )];
//        _goodLabel.font = [UIFont boldSystemFontOfSize:11];
//        _goodLabel.textColor = kCustomColor(180, 180, 180, 1);
//        [self addSubview: _goodLabel];
//        
//        
//        //  评论
////        _commentNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, self.bottom - 35 , 100 * WIDTH, 20 * HEIGHT)];
////        _commentNumLabel.textColor = kCustomColor(150, 150, 150, 1);
////        _commentNumLabel.font = [UIFont boldSystemFontOfSize:13];
////        [self addSubview:_commentNumLabel];
////        
////        //  最下方一条线
////        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bottom - 0.5 * HEIGHT, SCREEN_WIDTH, 0.5)];
////        _lineView.backgroundColor = kCustomColor(150, 150, 150, 1);
////        _lineView.alpha = 0.5;
////        [self addSubview:_lineView];
//        
//        
//    }
//    
//    return self;
//}

- (void)setGood:(NSString *)good
{
    _good = good;
    _goodLabel.text = _good;
}

- (void)setStatus:(int)status
{
    _status = status;
    
    //如果是已经收藏
    if (_status) {
        [_collectionButton setTitle:@"取消收藏" forState:(UIControlStateNormal)];
    }
    else
    {
        [_collectionButton setTitle:@"收藏" forState:(UIControlStateNormal)];
    }
}

- (void)setModel:(HomeDetialModel *)model
{
    _model = model;
    if (_model)
    {
        CGFloat width = [_model.imgweight floatValue];
        CGFloat height = [_model.imgheight floatValue];
        
        CGFloat imageHeight = height / width * (SCREEN_WIDTH - 20);
        
        //图片赋值并且重写设置高度
        [_backgroundImageView sd_setImageWithURL:[NSURL URLWithString:_model.img] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {}];
        _backgroundImageView.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, imageHeight);
        
        //整个头部的高度
//        CGFloat headHight = _backgroundImageView.height + _goodLabel.bottom + 15;
//        _headerHeight(headHight);
        //作者头像
        [_iconView sd_setImageWithURL:[NSURL URLWithString:_model.userFace] placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority];
        _tagLabel.text = _model.tags;
        _authorLabel.text = _model.nickName;
        _viewNumLabel.text = _model.views;
        _goodLabel.text = _model.good;
    }
    
    
    
    //头像的frame
    _iconView.frame = CGRectMake(10 * WIDTH, _backgroundImageView.bottom + 22, 30, 30);
    //标签的frame
    _tagLabel.frame = CGRectMake(_iconView.right + 10, _iconView.top , 100 * WIDTH, 14 * HEIGHT);
    //作者的名字
    _authorLabel.frame = CGRectMake(_iconView.right + 10, _tagLabel.bottom + 3, 100 * WIDTH, 12 * HEIGHT);
    //收藏按钮
    _collectionButton.frame = CGRectMake(SCREEN_WIDTH - 85 * WIDTH, _iconView.top, 75 * WIDTH, 30 * HEIGHT);
    
    //简介的frame
    _describeLabel.frame = CGRectMake(10, _iconView.bottom + 20, SCREEN_WIDTH - 20, [HomeDetialHeaderVIew heightOfString:_model.info]);
    
    //被查看的次数的图标
    _viewNumImageView.frame = CGRectMake(SCREEN_WIDTH - 120, _describeLabel.bottom + 20, 15, 12);
    //被查看的次数
    _viewNumLabel.frame = CGRectMake(_viewNumImageView.right + 3, _describeLabel.bottom + 20, 40, 11);
    
    //  点赞次数图标
    _goodNumImageView.frame = CGRectMake(_viewNumLabel.right + 3, _viewNumLabel.top, 15, 12);
    
    //  点赞次数Label
    _goodLabel.frame = CGRectMake(_goodNumImageView.right + 3, _goodNumImageView.top ,30, 11 );
    
    
    
    
}

+ (CGFloat)heightOfString:(NSString *)str
{
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName] context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightOfCell:(HomeDetialModel *)model
{
    if (model) {
        CGFloat width = [model.imgweight floatValue];
        CGFloat height = [model.imgheight floatValue];
        
        CGFloat imageHeight = height / width * ((SCREEN_WIDTH - 30) / 2);
        
        return [HomeDetialHeaderVIew heightOfString:model.info] + imageHeight + 120;
    }
    return 0;
    
}

+ (CGFloat)heightOfDetiallCell:(HomeDetialModel *)model
{
    if (model) {
        CGFloat width = [model.imgweight floatValue];
        CGFloat height = [model.imgheight floatValue];
        
        CGFloat imageHeight = height / width * (SCREEN_WIDTH - 20);
        
        return [HomeDetialHeaderVIew heightOfString:model.info] + imageHeight + 120;
    }
    return 0;
    
}

- (void)tapToCollect:(UIButton *)button
{
    button.selected = !button.isSelected;

    if (_status) {
        [_collectionButton setTitle:@"收藏" forState:(UIControlStateNormal)];
        _collectionButton.backgroundColor = kCustomColor(40, 40, 50, 1);
        [_collectionButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        [self requestCollect];
        _status = 0;
    }
    else
    {
        [_collectionButton setTitle:@"取消收藏" forState:(UIControlStateNormal)];
        [self requestCollect];
        _status = 1;
    }
}

/**
 *  添加/取消收藏图片
 */
- (void)requestCollect
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = nil;
    if (uid && key) {
        pardic = @{@"imgId":_model.Id,
                   @"uid":uid,
                   @"key":key};
    }
    
    [manage POST:kURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             
             // 请求成功的时候调用这个block
             NSLog(@"数据请求-------------------%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}


//+ (CGFloat)heightOfHeader:(HomeDetialModel *)model
//{
//    
//    CGFloat width = [model.imgweight floatValue];
//    CGFloat height = [model.imgheight floatValue];
//    
//    CGFloat imageHeight = height / width * ((SCREEN_WIDTH - 30) / 2);
//    
//    
//    return 104 + imageHeight ;
//}
- (NSUInteger )supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}




#pragma mark --单击查看图片方法
- (void)bigButtonTapped:(id)sender {
    
    // Create image info
    JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
    imageInfo.image = _backgroundImageView.image;
    imageInfo.referenceRect = _backgroundImageView.frame;
    imageInfo.referenceView = _backgroundImageView.superview;
    imageInfo.referenceContentMode = _backgroundImageView.contentMode;
    imageInfo.referenceCornerRadius = _backgroundImageView.layer.cornerRadius;
    
    // Setup view controller
    JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
                                           initWithImageInfo:imageInfo
                                           mode:JTSImageViewControllerMode_Image
                                           backgroundStyle:JTSImageViewControllerBackgroundOption_None];
    // Present the view controller.
    [imageViewer showFromViewController:self.viewController transition:JTSImageViewControllerTransition_FromOriginalPosition];
}


@end
